package com.cvte.boe.media.views.settingpage;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.cvte.boe.media.R;
import com.cvte.boe.media.adapter.RecyclerViewClickAndSelectAdapter;
import com.cvte.boe.media.dao.MediaSettingDao;
import com.cvte.boe.media.dao.MediaSettingDaoImpl;
import com.cvte.boe.media.utils.LogUtil;
import com.cvte.boe.media.utils.SettingUtil;
import com.cvte.boe.media.views.widget.ComboView;

/**
 * Created by wuguitong on 2018/5/3.
 */
public class PageVideoPlayerSettings extends LinearLayout implements ComboView.ComboViewUpdateListener {
    private ComboView loopComboView;
    private ComboView subComboView;
    private ComboView audioComboView;
    private ComboView displayComboView;
    private Context mContext;
    private MediaSettingDao dao;
    private OnVideoSettingListener listener = null;

    public interface OnVideoSettingListener {
        boolean onUpdateVideoLoop(SettingUtil.VLOOP loop);
        boolean onUpdateVideoSub(SettingUtil.VSUBTITLE sub);
        boolean onUpdateVideoAudio(SettingUtil.VAUDIO audio);
        boolean onUpdateVideoDisplay(SettingUtil.VDISPLAY display);
    }
    public PageVideoPlayerSettings(Context context) {
        this(context,null);
    }

    public PageVideoPlayerSettings(Context context, AttributeSet attrs) {
        this(context, null,0);
    }

    public PageVideoPlayerSettings(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        dao = new MediaSettingDaoImpl(mContext);
        inflate(mContext, R.layout.page_video_player_setting,this);
        loopComboView = (ComboView)findViewById(R.id.cv_video_loop_setting);
        loopComboView.initList(SettingUtil.getList(mContext,SettingUtil.VIDEOLOOP));
        loopComboView.setCurrentIndex(dao.getVideoLoop().ordinal());
        loopComboView.setComboViewListener(this);

        subComboView = (ComboView)findViewById(R.id.cv_video_sub_setting);
        subComboView.initList(SettingUtil.getList(mContext,SettingUtil.VIDEOSUBTITLE));
        subComboView.setCurrentIndex(dao.getVideoSubtitle().ordinal());
        subComboView.setComboViewListener(this);

        audioComboView = (ComboView)findViewById(R.id.cv_video_audio_setting);
        audioComboView.initList(SettingUtil.getList(mContext,SettingUtil.VIDEOAUDIO));
        audioComboView.setCurrentIndex(dao.getVideoAudio().ordinal());
        audioComboView.setComboViewListener(this);

        displayComboView = (ComboView)findViewById(R.id.cv_video_display_setting);
        displayComboView.initList(SettingUtil.getList(mContext,SettingUtil.VIDEODISPLAY));
        displayComboView.setCurrentIndex(dao.getVideoDisplay().ordinal());
        displayComboView.setComboViewListener(this);

        loopComboView.setSelected(true);
    }
    public void setOnVideoPlayerListener(OnVideoSettingListener listener){
        this.listener = listener;
    }
    @Override
    public boolean onUpdateListener(ComboView view, int index) {
        boolean ret = false;
        if(listener != null) {
            switch (view.getId()) {
                case R.id.cv_video_loop_setting:
                    ret = listener.onUpdateVideoLoop(SettingUtil.VLOOP.values()[index]);
                    if(ret) {
                        dao.setVideoLoop(SettingUtil.VLOOP.values()[index]);
                    }
                    break;
                case R.id.cv_video_sub_setting:
                    ret = listener.onUpdateVideoSub(SettingUtil.VSUBTITLE.values()[index]);
                    if(ret) {
                        dao.setVideoSubtitle(SettingUtil.VSUBTITLE.values()[index]);
                    }
                    break;
                case R.id.cv_video_audio_setting:
                    ret = listener.onUpdateVideoAudio(SettingUtil.VAUDIO.values()[index]);
                    if(ret) {
                        dao.setVideoAudio(SettingUtil.VAUDIO.values()[index]);
                    }
                    break;
                case R.id.cv_video_display_setting:
                    ret = listener.onUpdateVideoDisplay(SettingUtil.VDISPLAY.values()[index]);
                    if(ret) {
                        dao.setVideoDisplay(SettingUtil.VDISPLAY.values()[index]);
                    }
                    break;
                default:
                    break;
            }
        }
        return ret;
    }
}
